﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using r2mp_DataStructures;

namespace r2mp_GUI
{
    
    /// <summary>
    /// Form used to draw the diagrams in and also manages saving and printing
    /// these diagrams.
    /// </summary>
    public partial class R2mpVisualizer : Form
    {
        DiagramWorker worker;

        /// <summary>
        /// Constructs the form for a PSP-Diagram
        /// </summary>
        /// <param name="title">Title of the form</param>
        /// <param name="tasklist">The tasklist to create the PSP-Diagram from</param>
        public R2mpVisualizer(String title, Task tasklist) : this(title)
        {
            #region hardcoded testdata
            //DEBUG CODE
            //tasklist = new Task("R2MP", "1", -1, 1);

            //tasklist.Tasks.Add(new Task("PM", "1.1", -1, 1));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt ist gestartet", "1.1.1", 0, 2));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt planen", "1.1.2", 14, 2));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt koordinieren", "1.1.3", 27, 1));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt controllen", "1.1.4", 27, 1));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt abschließen", "1.1.5", 5, 1));
            //tasklist.Tasks[0].Tasks.Add(new Task("Projekt ist abgenommen", "1.1.6", 5, 1));

            //tasklist.Tasks.Add(new Task("Recherche", "1.2", -1, 2));
            //tasklist.Tasks[1].Tasks.Add(new Task("Recherche von MPP-Date und Auslesefunktionen", "1.2.1", -1, 2));
            //tasklist.Tasks[1].Tasks.Add(new Task("Recherche Funktionsweise MS Office Plugins", "1.2.2", 4, 2));

            //tasklist.Tasks.Add(new Task("Konzeption", "1.3", -1, 1));
            //tasklist.Tasks[2].Tasks.Add(new Task("Erzeugen des Microsoft Project Plugins", "1.3.1", -1, 2));
            //tasklist.Tasks[2].Tasks.Add(new Task("Erstellen der Klassen und Datenstrukturen", "1.3.2", -1, 2));
            //tasklist.Tasks[2].Tasks.Add(new Task("Einlesen", "1.3.3", -1, 2));
            //tasklist.Tasks[2].Tasks[2].Tasks.Add(new Task("MPP Daten verarbeiten", "1.3.3.1", -1, 2));
            //tasklist.Tasks[2].Tasks[2].Tasks.Add(new Task("Excel Daten einlesen und verarbeiten", "1.3.3.2", -1, 2));
            //tasklist.Tasks[2].Tasks.Add(new Task("Erstellen des PSP", "1.3.3", -1, 1));
            //tasklist.Tasks[2].Tasks.Add(new Task("Erstellen der MTA", "1.3.4", -1, 1));
            //tasklist.Tasks[2].Tasks.Add(new Task("Anzeige der Grafiken", "1.3.5", -1, 2));
            //tasklist.Tasks[2].Tasks.Add(new Task("Speicherfunktion", "1.3.6", -1, 2));

            //tasklist.Tasks.Add(new Task("Testen", "1.4", 20, 1));
            //tasklist.Tasks[3].Tasks.Add(new Task("Testen des Plugins", "1.4.3", 5, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks.Add(new Task("Testen der einzelnen Funktionen", "1.4.3.1", 5, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks.Add(new Task("Testen des PSP's", "1.4.1", -1, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks[0].Tasks.Add(new Task("Skalieren", "1.4.1.1", -1, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks[0].Tasks.Add(new Task("Extremtest", "1.4.1.2", 12, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks.Add(new Task("Testen des MTA", "1.4.2", 12, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks[1].Tasks.Add(new Task("Raster", "1.4.2.1", 5, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks[1].Tasks.Add(new Task("dynamische Größe", "1.4.2.2", 5, 1));
            //tasklist.Tasks[3].Tasks[0].Tasks[0].Tasks[1].Tasks.Add(new Task("Linienführung", "1.4.2.3", 5, 1));
            //DEBUG CODE END
            #endregion
            worker = new PSPWorker(tasklist, drawingArea);
        }

        /// <summary>
        /// Constructs the form for a MTA-Diagram
        /// </summary>
        /// <param name="title">Title of the form</param>
        /// <param name="milestonelist">The milestonelist to create the MTA-Diagram from</param>
        public R2mpVisualizer(String title, MTA milestonelist) : this(title)
        {
            worker = new MTAWorker(milestonelist, drawingArea);
        }

        /// <summary>
        /// Constructs the basic form and sets an eventhandler for Mousewheel-actions
        /// </summary>
        /// <param name="title">Title of the form</param>
        public R2mpVisualizer(String title)
        {
            InitializeComponent();
            this.Text = title;
            this.MouseWheel += new MouseEventHandler(r2mpVisualizer_MouseWheel);
        }

        /// <summary>
        /// Calls the worker-method to zoom in or out in reaction to the use of the mousewheel onto the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void r2mpVisualizer_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                worker.zoomOut();
            }
            else if (e.Delta > 0)
            {
                worker.zoomIn();
            }
            drawingArea.Invalidate();
        }

        /// <summary>
        /// Draws the diagram using the worker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void drawingArea_Paint(object sender, PaintEventArgs e)
        {
            worker.draw(e.Graphics);
        }

        /// <summary>
        /// Takes care of saving the current diagram in a bitmap file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void speichernToolStripButton_Click(object sender, EventArgs e)
        {
            SaveDiagram.SaveBitmap(drawingArea);
        }

        /// <summary>
        /// Refreshes the panel in case of scrolling
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void scrollPanel_Scroll(object sender, ScrollEventArgs e)
        {
            drawingArea.Invalidate();
        }

        /// <summary>
        /// Refreshes the panel in case of resizing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void R2mpVisualizer_ResizeEnd(object sender, EventArgs e)
        {
            drawingArea.Invalidate();
        }
    }
}
